package com.jiutong.meipengdai.ui.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.entity.LoginOrRegisterRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.model.requests.UserInfoRequest;
import com.jiutong.meipengdai.http.present.UserInfoPresent;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作用: 重置登录密码
 * Created by 78 on 2017/6/17.
 */
public class ChangePwdActivity extends BaseActivity implements RegisterAndLoginBiz {

    @Bind(R.id.tv_title)
    TextView tv_title;
    @Bind(R.id.et_pwd_reset_new_pwd)
    EditText et_pwd_reset_new_pwd;
    @Bind(R.id.et_pwd_reset_new_pwd_again)
    EditText et_pwd_reset_new_pwd_again;
    @Bind(R.id.btn_confirm)
    Button btn_confirm;
    private UserInfoPresent userInfoPresent;
    private String old_pwd;
    private String identifyCode;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);

        tv_title.setText(R.string.reset_pwd);

        Bundle bundle = getIntent().getExtras();
        if(bundle == null)
            return;
        if(!TextUtils.isEmpty(bundle.getString("pwd"))){
            old_pwd = bundle.getString("pwd");
        }if(!TextUtils.isEmpty((bundle.getString("identifyCode")))){
            identifyCode = bundle.getString("identifyCode");
        }
        btn_confirm.setEnabled(false);
        EditTextChange etc = new EditTextChange();
        et_pwd_reset_new_pwd.addTextChangedListener(etc);
        et_pwd_reset_new_pwd_again.addTextChangedListener(etc);


        userInfoPresent = new UserInfoPresent(this, this);
    }
    class EditTextChange implements TextWatcher{

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            boolean isPwdOk = getPassword().length()>=8&&getPassword().length()<=16;
            boolean isRePwdOk = getNewPwd().length()>=8&&getNewPwd().length()<=16;
            if(isPwdOk&isRePwdOk){
                btn_confirm.setBackgroundResource(R.drawable.selector_orange_btn_bg);
                btn_confirm.setEnabled(true);
            }else{
                btn_confirm.setBackgroundResource(R.drawable.shape_light_gray_btn);
                btn_confirm.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @OnClick({R.id.rl_back, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_confirm:
                if (isRightAction()) {
                    //当传过来的旧密码为空的时候，表示为手机验证码修改
                    if (TextUtils.isEmpty(old_pwd)) {
                        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
                            @Override
                            public void onNext(BaseRespond subjects) {
                                BaseRespond respond = (BaseRespond)subjects;
                                if(respond.getResCode().equals("200")) {
                                    toastShortShow(respond.getResDesc());
                                    Bundle bundle = new Bundle();
                                    bundle.putString("new_pwd", getNewPwd());
                                    openActivity(AccountSettingActivity.class, bundle);
                                    finish();
                                }else{
                                    toastShortShow(respond.getResDesc());
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                            }
                        };

                        HashMap<String, Object> fieldsMap = new HashMap<>();
                        fieldsMap.put("code", identifyCode);
                        fieldsMap.put("newPwd",getNewPwd());

                        new UserInfoRequest().pwdResetByCode(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
                    } else {
                        userInfoPresent.pwdResetByOldpwd();
                    }
                }
                break;
        }
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {

        if (TextUtils.isEmpty(getPassword())) {
            toastShortShow("请输入新密码");
            return false;
        }

        if (getPassword().length() < 8 || getPassword().length() > 16) {
            toastShortShow("请输入8-16位数的密码");
            return false;
        }

        if (TextUtils.isEmpty(getNewPwd())) {
            toastShortShow("请输入确认密码");
            return false;
        }

        if (!getNewPwd().equals(et_pwd_reset_new_pwd.getText().toString())) {
            toastShortShow("两次输入密码不一致，请重新输入");
            return false;
        }
//        if (old_pwd.equals(et_pwd_reset_new_pwd.getText().toString())) {
//            toastShortShow("新密码与原密码相同");
//            return false;
//        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return null;
    }

    @Override
    public String getPassword() {
        return et_pwd_reset_new_pwd.getText().toString().trim();
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return old_pwd;
    }

    @Override
    public String getNewPwd() {
        return et_pwd_reset_new_pwd_again.getText().toString().trim();
    }

    @Override
    public String getRelName() {
        return null;
    }

    @Override
    public String getIdCard() {
        return null;
    }

    @Override
    public String getEmail() {
        return null;
    }

    @Override
    public String getNewPhone() {
        return null;
    }

    @Override
    public String getCardNumber() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(IdentifyCodeRespond subjects) {

    }

    @Override
    public void onSuccess(Object object) {
        LoginOrRegisterRespond respond = (LoginOrRegisterRespond) object;
        if (respond.getResCode().equals("200")) {
            toastShortShow(respond.getResDesc());
            openActivity(AccountSettingActivity.class);
            finish();
        }else{
            toastShortShow(respond.getResCode());
        }
    }

    @Override
    public void onError(Throwable e) {

    }
}
